package com.test.controller;

import com.test.domain.User;
import com.test.service.DeptService;
import com.test.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * CommonController是专门存放不需要登录认证的请求，全部放在这里
 */
@Controller
public class CommonController {
	@Resource
	HttpServletRequest request;
	
	@Resource
	UserService userService;
	
	@Resource
	DeptService deptService;
	
	/**
	 * 首页-跳转到登录页面
	 * @return
	 */
	@GetMapping("/")
	public String loginPage(){
		return "login";
	}
	
	/**
	 * 登录业务--登录过程
	 * @return
	 */
	@PostMapping("/common/login")
	public @ResponseBody Map<String,Object> login(String username,String password){
		Map<String,Object> data=new HashMap<>();
		User user = userService.login(username, password);
		if(user==null){
			data.put("flag", false);
			data.put("msg","账号或者密码不正确");
		}else {
			data.put("flag", true);
			//把当前的登录账号信息存入session
			request.getSession().setAttribute("user", user);
		}
		return data;
	}
	
	/**
	 * 登录业务--登录过程
	 * @return
	 */
	@PostMapping("/app/login")
	public @ResponseBody Map<String,Object> login1(String username,String password){
		Map<String,Object> data=new HashMap<>();
		User user = userService.login(username, password);
		if(user==null){
			data.put("flag", false);
			data.put("msg","账号或者密码不正确");
		}else {
			data.put("flag", true);
			//把当前的登录账号信息存入session
			data.put("user", user);
		}
		return data;
	}
}